package cn.j229.bug.zhongxuan.hualian.controller;

import cn.j229.bug.zhongxuan.hualian.exception.BusinessException;
import cn.j229.bug.zhongxuan.hualian.pojo.Manager;
import cn.j229.bug.zhongxuan.hualian.service.ManagerService;
import cn.j229.bug.zhongxuan.hualian.utils.JsonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author smile_ZhongXuan
 * @Date 2020/10/23 9:59
 * @Version 1.0
 */
@RestController
@RequestMapping("/manager")
public class ManagerController {

    @Resource
    ManagerService managerService = null;
    JsonResult jsonResult = new JsonResult();

    @PostMapping("/addManager")
    public Map addManager(@RequestBody Manager manager){
        Manager findManagerByPhone = managerService.loginManager(manager.getPhone());
        if(findManagerByPhone == null || findManagerByPhone.equals("")){
            managerService.addManager(manager);
            jsonResult.setCode("200");
            jsonResult.setMsg("新增管理员用户成功。");
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("该管理员已存在，请更换手机号后重试。");
            throw new BusinessException("该手机号在数据库中已存在，请更换手机号重试。");
        }
        return jsonResult.getValues();
    }

    @PostMapping("/login")
    public Map loginManager(@RequestParam("phone") String phone){
        Manager manager = managerService.loginManager(phone);
        if(manager == null || manager.equals("")){
            jsonResult.setCode("0");
            jsonResult.setMsg("用户不存在，登录失败。");
            throw new BusinessException("用户在数据库中不存在。");
        }
        if(manager.getState() == false || manager.getState().equals(false)){
            jsonResult.setCode("0");
            jsonResult.setMsg("登录失败，该账户处于冻结状态，请联系超级管理员进行相关授权操作。");
            throw new BusinessException("该管理员用户处于冻结状态，无法进行登录。");
        }else{
            jsonResult.setCode("200");
            jsonResult.setMsg("登录成功！");
        }
        return jsonResult.getValues();
    }

    @PostMapping("/managerUpdateState")
    public Map managerUpdateState(@RequestParam("phone") String phone,@RequestParam("state") Boolean state){
        int managerUpdateState = managerService.managerUpdateState(phone,state);
        if(managerUpdateState == 0){
            jsonResult.setCode("0");
            jsonResult.setMsg("更新失败，该手机账号未找到。");
            throw new BusinessException("更新失败，该手机号在数据库中不存在。");
        }
        jsonResult.setCode("200");
        jsonResult.setMsg("账户状态更新成功。");
        return jsonResult.getValues();
    }

    @PostMapping("/managerDelete")
    public Map managerDelete(@RequestParam("phone") String phone){
        int managerDelete = managerService.managerDelete(phone);
        if(managerDelete == 0){
            jsonResult.setCode("0");
            jsonResult.setMsg("删除失败，该手机号管理员不存在。");
            throw new BusinessException("该手机号的管理员在数据库中不存在。");
        }
        jsonResult.setCode("200");
        jsonResult.setMsg("删除管理员用户成功。");
        return jsonResult.getValues();
    }

    @PostMapping("/findManagerListByLike")
    public Map findManagerListByLike(@RequestParam("field") String field,@RequestParam("page") Integer page,@RequestParam("size") Integer size){
        Manager manager = new Manager();
        manager.setName(field);
        manager.setPhone(field);
        // 声明：page为页数，size为每页中显示多少条记录
        PageHelper.startPage(page,size);
        // 做列表查询，得出查询结果列表
        List managerListByLike = managerService.findManagerListByLike(manager);
        // 将查询结果带入到分页条件中去
        PageInfo<Manager> pageInfo = new PageInfo<>(managerListByLike);
        // 获取分页后的列表数据
        List<Manager> pageInfoList = pageInfo.getList();
        // 获取总记录数
        long totalRecords = pageInfo.getTotal();
        jsonResult.setCode("200");
        jsonResult.setData(pageInfoList);
        jsonResult.setMsg("查询成功，共" + totalRecords + "条数据。");
        return jsonResult.getValues();
    }
}
